package cn.darkhorse.mesadmin.controller.device;


import cn.darkhorse.device.pojo.MesOilCategory;
import cn.darkhorse.device.service.MesOilCategoryService;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.service.ISysDictDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 油耗分类 前端控制器
 * </p>
 *
 * @author yangtianqi
 * @since 2024-11-06
 */
@RestController
@RequestMapping("/device/mesOilCategory")
@Api(tags = "油耗分类管理")
public class MesOilCategoryController extends BaseController {
    @Autowired
    private MesOilCategoryService mesOilCategoryService;
    @Autowired
    private ISysDictDataService dictDataService;
    @GetMapping("/list")
    @ApiOperation("列表")
    public TableDataInfo list(MesOilCategory mesOilCategory){
        startPage();
        List<MesOilCategory> list = mesOilCategoryService.list(mesOilCategory);
        for (MesOilCategory oilCategory : list) {
            if (oilCategory.getType()!=null){
                oilCategory.setTypeName(dictDataService.selectDictLabel("oil_type", oilCategory.getType()));
            }
        }
        return getDataTable(list);
    }

    @GetMapping("/delete")
    @ApiOperation("删除")
    public AjaxResult delete(Long id){
        if (mesOilCategoryService.delete(id)) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @PostMapping("/save")
    @ApiOperation("保存")
    public AjaxResult saveOilCategory(@RequestBody MesOilCategory mesOilCategory){
        if (mesOilCategoryService.saveMesOilCategory(mesOilCategory)) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @GetMapping("/view")
    @ApiOperation("详情")
    public AjaxResult view(Long id){
        MesOilCategory category = mesOilCategoryService.view(id);
        if (category!=null){
            category.setTypeName(dictDataService.selectDictLabel("oil_type", category.getType()));
        }
        return AjaxResult.success(category);
    }
    @ApiOperation(value = "获取编码")
    @RequestMapping("/getCode")
    public AjaxResult getCode() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",System.currentTimeMillis());
        return AjaxResult.success(jsonObject);
    }

}

